package cc.jethro.jdxyq.fragment

import android.os.Bundle
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup

import com.youth.banner.Banner


import java.util.ArrayList

import cc.jethro.jdxyq.banner.GlideImageLoader
import cc.jethro.jdxyq.R
import cc.jethro.jdxyq.homeActividadesRecycler.Actividades
import cc.jethro.jdxyq.homeActividadesRecycler.ActividadesAdapter
import cc.jethro.jdxyq.homeFunctionRecycler.HomeEntrance
import cc.jethro.jdxyq.homeFunctionRecycler.HomeEntranceAdapter
import cc.jethro.jdxysdk.util.AssetsUtil


/**
 * Created by jethr on 2017/5/10.
 */

class HomeFragment : BaseFragment() {
    private var images: ArrayList<Int>? = null
    private val homeEntrances = ArrayList<HomeEntrance>()
    private val actividadesArrayList = ArrayList<Actividades>()

    var banner : Banner? = null
    var homeFunRecyclerView : RecyclerView ?= null
    //主页的功能入口
    var homeAcRecyclerView :RecyclerView ?= null

    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        val view = inflater!!.inflate(R.layout.fragment_home, container, false)

        initView(view = view) //初始化homeFragment中的view
        setBanner() //设置首页轮播图
        initHomeEntranceData() //初始化首页功能数据

        //设置首页功能入口
        //调用recycler
        val gridLayoutManager = GridLayoutManager(activity, 5)
        homeFunRecyclerView!!.layoutManager = gridLayoutManager
        homeFunRecyclerView!!.adapter = HomeEntranceAdapter(context, homeEntrances)
        //解决recycler在scrollview中惯性失效的问题
        homeFunRecyclerView!!.isNestedScrollingEnabled = false

        //设置首页活动入口
        initHomeActividadesData()
        homeAcRecyclerView!!.layoutManager = GridLayoutManager(context, 2)
        homeAcRecyclerView!!.adapter = ActividadesAdapter(context, actividadesArrayList)
        homeAcRecyclerView!!.isNestedScrollingEnabled = false

        return view
    }

    /**
     * 初始化视图

     * @param view
     */
    private fun initView(view: View) {
        banner = view.findViewById(R.id.banner_view) as Banner?
        homeFunRecyclerView = view.findViewById(R.id.view_function_entrance_recycler) as RecyclerView?
        homeAcRecyclerView = view.findViewById(R.id.view_actividades_recycler) as RecyclerView?
    }

    /**
     * 设置轮播图
     */
    private fun setBanner() {
        banner!!.setImageLoader(GlideImageLoader())
        images = ArrayList<Int>()
        images!!.add(R.drawable.one)
        images!!.add(R.drawable.two)
        images!!.add(R.drawable.three)
        images!!.add(R.drawable.fouer)
        banner!!.setImages(images!!)
        banner!!.start()
    }

    /**
     * 初始化主页功能图标标题的是数据
     */
    private fun initHomeEntranceData() {
        val chaoshi = HomeEntrance("在线超市",
                AssetsUtil.getUri(this.context, "ic-fun/ic_fun_chaoshi.jpg"))
        val ganxi = HomeEntrance("衣物干洗",
                AssetsUtil.getUri(this.context, "ic-fun/ic_fun_ganxi.png"))
        val kuaidi = HomeEntrance("快递代取",
                AssetsUtil.getUri(this.context, "ic-fun/ic_fun_kuaidi.png"))
        val waimai = HomeEntrance("在线外卖",
                AssetsUtil.getUri(this.context, "ic-fun/ic_fun_waimai.png"))
        val tiaozhao = HomeEntrance("跳蚤市场",
                AssetsUtil.getUri(this.context, "ic-fun/ic_fun_tiaozhao.png"))
        val zhaoling = HomeEntrance("失物招领",
                AssetsUtil.getUri(this.context, "ic-fun/ic_fun_zhaoling.jpg"))
        val zy = HomeEntrance("资源共享",
                AssetsUtil.getUri(this.context, "ic-fun/ic_fun_ziyuan.png"))
        val jz = HomeEntrance("工作兼职",
                AssetsUtil.getUri(this.context, "ic-fun/ic_fun_jianzhi.png"))
        val xc = HomeEntrance("我要学车",
                AssetsUtil.getUri(this.context, "ic-fun/ic_fun_car.jpg"))
        val ben = HomeEntrance("我要升本",
                AssetsUtil.getUri(this.context, "ic-fun/ic_fun_ben.jpg"))

        homeEntrances.add(chaoshi)
        homeEntrances.add(ganxi)
        homeEntrances.add(kuaidi)
        homeEntrances.add(waimai)
        homeEntrances.add(tiaozhao)
        homeEntrances.add(zhaoling)
        homeEntrances.add(zy)
        homeEntrances.add(jz)
        homeEntrances.add(xc)
        homeEntrances.add(ben)
    }


    /**
     * 初始化主页活动标题等数据
     */
    private fun initHomeActividadesData() {
        val context = context
        val yushan = Actividades("爱心雨伞", "活动 准备中",
                AssetsUtil.getUri(context, "ic-ac/shan.png"))
        val qian = Actividades("签到有礼", "礼物 送不停",
                AssetsUtil.getUri(context, "ic-ac/qian.png"))
        val zhong = Actividades("学院众筹", "项目 一起做",
                AssetsUtil.getUri(context, "ic-ac/zhong.png"))
        val shequ = Actividades("交流社区", "一起 讨论吧",
                AssetsUtil.getUri(context, "ic-ac/shequ.png"))
        val yiyuan = Actividades("一元购", "一元 就购了",
                AssetsUtil.getUri(context, "ic-ac/yiyuan.png"))
        val love = Actividades("恋聊吧", "认真 谈恋爱",
                AssetsUtil.getUri(context, "ic-ac/love.png"))
        val pipa = Actividades("琵琶熟了", "低至 4.5元/斤",
                AssetsUtil.getUri(context, "ic-ac/pipa.png"))





        actividadesArrayList.add(yushan)
        actividadesArrayList.add(qian)
        actividadesArrayList.add(zhong)
        actividadesArrayList.add(shequ)
        actividadesArrayList.add(yiyuan)
        actividadesArrayList.add(love)
        actividadesArrayList.add(pipa)
    }
}
